package logic

import (
	"context"
	"errors"
	"houseRpc/house"
	"houseRpc/internal/svc"
	"houseRpc/models"
	"houseRpc/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameUserLogic {
	return &RealNameUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealNameUserLogic) RealNameUser(in *house.RealNameUserRequest) (*house.RealNameUserResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var u models.User
	var ur models.RealName
	u.ID = uint(in.UserId)
	ur.UserId = in.UserId

	if err = ur.GetRealNameByUserId(); err != nil {
		logx.Error("用户信息获取失败")
		return nil, errors.New("用户信息获取失败")
	}

	if ur.ID != 0 {
		logx.Error("当前用户已实名")
		return nil, errors.New("当前用户已实名")
	}

	ur.RealName = in.RealName
	ur.IdCard = in.IdCare

	if !pkg.AuThRealName(in.IdCare, in.RealName) {
		logx.Error("实名认证失败:第三方接口调用")
		return nil, errors.New("实名认证失败:第三方接口调用")
	}

	if err = ur.CreateRealName(); err != nil {
		logx.Error("用户实名认证失败")
		return nil, errors.New("用户实名认证失败")

	}

	return &house.RealNameUserResponse{
		UserId: int64(u.ID),
	}, nil
}
